package cn.edu.ctbu.labtest.service.impl;

import cn.edu.ctbu.labtest.dao.ScRepository;
import cn.edu.ctbu.labtest.domain.Sc;
import cn.edu.ctbu.labtest.service.ScService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ScImplService implements ScService {

    @Autowired
    private ScRepository scRepository;

    /**
     * 分页，带过滤的查询
     */
    public Page<Sc> findAll(Example<Sc> sc, Pageable pageable){
        return scRepository.findAll(sc,pageable);
    }

    /**
     * 只分页的查询
     */
    public Page<Sc> findAll(Pageable pageable){
        return scRepository.findAll(pageable);
    }

    /**
     * 读取所有的成绩数据
     * @return
     */
    public List<Sc> findAll(){
        return scRepository.findAll();
    }

    /**
     * 更新学生成绩
     */
    public Sc update(Sc sc){
        return scRepository.save(sc);
    }

    /**
     * 新增学生成绩
     */
    public Sc insert(Sc sc){
        return scRepository.save(sc);
    }

    /**
     * 删除学生成绩
     */
    public void delete(Integer id){
        Sc sc=new Sc();
        sc.setId(id);
        scRepository.delete(sc);
    }

    /**
     * 通过id查找
     * @param id
     * @return
     */
    public Sc getById(Integer id){
        Sc sc=scRepository.findById(id).orElse(null);
        return sc;
    }

    /**
     * 通过名字模糊查询学生成绩
     * @param name
     * @param pageable
     * @return
     */
    public Page<Sc> findScByNameLike(String name,Pageable pageable){
        Sc sc=new Sc();
        sc.setName(name);
        return scRepository.findScByNameLike(name, pageable);
    }

    /**
     * 通过名字查询学生成绩
     */
    public List<Sc> findScByName(String name){
        Sc sc=new Sc();
        sc.setName(name);
        return scRepository.findScByName(name);
    }
}
